package com.smart.tv.boundary;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.smart.tv.R;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListMenu extends ListActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_menu);

		music = new Music("music");
		fillAllList();
		Intent intent = getIntent();
		statusMenu = intent.getStringExtra(STATUS_MENU);
		TextView txtTitle = (TextView) findViewById(R.id.title);
		txtTitle.setText(statusMenu);
		ListView listView = getListView();
		listView.setAdapter(new ListAdapter(this, R.layout.list_item));
		
		if(listMenu!=null){
			listView.setSelection(listMenu.size()-1);
		}
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				
				Intent intent = new Intent();
				intent.setAction(android.content.Intent.ACTION_VIEW);
				
				File file = new File("/sdcard/music/"+listMenu.get(position));
				Log.d("ListMenu", "file : "+file.getAbsolutePath()+" is file?"+file.isFile());
				intent.setDataAndType(Uri.fromFile(file), "audio/mp3");
				try{
					startActivity(intent); 
				
				}catch(Exception e){
				  e.printStackTrace();
				}
				
				
				
			}
		});
		
	}
	
	private void fillAllList(){
		String [] names = music.getAllFileName();
		
		for(String name : names){
			listMenu.add(name);
		}
	}

	public static final String CHANNEL 	= "Channel";
	public static final String LAYANAN 	= "Layanan";
	public static final String FOOD 	= "Makanan";
	public static final String DRINK 	= "Minuman";
	public static final String MUSIC	= "music";
	
	public static final String STATUS_MENU = "statusMenu";
	
	private String statusMenu = "";
	private List<String> listMenu = new ArrayList<String>();
	
	
	private class ListAdapter extends BaseAdapter{

		public ListAdapter(Context contextParam, int resourceIdParam){
			context = contextParam;
			resourceId = resourceIdParam;
		}
		public int getCount() {
			if(listMenu == null) return 0;
			return listMenu.size();
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			
			if(listMenu == null || listMenu.size()<=0) return null;
			if (row == null) {
				LayoutInflater inflater = (LayoutInflater) this.context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
				row = inflater.inflate(resourceId, parent, false);
			}
			
			TextView txtItem = (TextView) row.findViewById(R.id.txtItem);
			txtItem.setText(listMenu.get(position));
			
			return row;
		}
		
		private int resourceId = 0;
		private Context context = null;
		
	}
	
	private Music music;
}
